# Extensions by Glenn S. West 
# Extra methods to allow a more "direct" mapping" from pdf-reader
require 'pp'

class PDF::Writer
    #Hacked add image to not mess up coordinate space - gsw
    def myimage(image, label)

      if image.respond_to?(:read)
        data = image.read
       else
        open(image, 'rb') { |ff| data = ff.read }
        end

      image_info ||= PDF::Writer::Graphics::ImageInfo.new(data)
      tt = Time.now
      @images << tt
      id = @images.index(tt)
      image_obj = PDF::Writer::External::Image.new(self, data, image_info, label)
      @images[id] = image_obj

      image_obj
      end

  def save_graphics_state()
      add_content("\nq")
      end

  def restore_graphics_state()
      add_content("\nQ")
      end

  def concatenate_matrix(a,b,c,d,e,f)
      add_content("\n%f %f %f %f %f %f cm" % [ a, b, c, d, e, f])
      @pdfeo_matrix = Array.[](a,b,c,d,e,f)
      end

  def invoke_xobject(form,name)
       dir_name = @rubyclass.to_s
       png_name = form + "/" + name + ".png"
       jpg_name = form + "/" + name + ".jpg"
       if File.exists?(jpg_name)
          the_name = jpg_name
          end
       if File.exists?(png_name)
          the_name = png_name
          end
       myimage(the_name, name)
       add_content("\n/#{name} Do")
      end

  # Page 405 - PDF Reference
  def begin_text_object()
      add_content("\nBT")
      end

  def set_character_spacing(value)
      add_content("\n #{value} Tc")
      end

  def set_word_spacing(value)
      add_content("\n #{value} Tw")
      end

  def set_text_font_and_size(fontabbr,fontsize)
      add_content("\n/#{fontabbr} #{fontsize} Tf")
      end

  def end_text_object()
      add_content("\nET")
      end

  def show_text(txt)
      thetxt = txt.gsub('\\','\\\\')
      thetxt = thetxt.gsub('(','\(')
      thetxt = thetxt.gsub(')','\)')
      add_content("\n(#{thetxt})Tj")
      end

  # set_line_matrix_and_text_line_matrix
  def set_text_matrix_and_text_line_matrix(a, b, c, d, e, f)
    add_content("\n%f %f %f %f %f %f Tm" % [ a, b, c, d, e, f ])
  end

  def begin_marked_content(value)
      add_content("\n/#{value} BMC")
      end

  def begin_marked_content_with_p1(flag, mcid_type, mcid_value)
      add_content("\n/#{flag} << /#{mcid_type} #{mcid_value} >>\nBDC")
      end
  
  #Page 679 - PDF Reference
  def end_marked_content()
      add_content("\nEMC")
      end

  def append_rectangle(a,b,c,d)
      add_content("\n#{a} #{b} #{c} #{d} re")
      end

  def fill_path_with_nonzero()
      add_content("\nf")
      end

  def begin_new_subpath(a,b)
      add_content("\n#{a} #{b} m")
      end

  def append_line(a,b)
      add_content("\n#{a} #{b} l")
      end

  #{:name=>:set_gray_for_stroking, :args=>[0.0]}
  def set_gray_for_stroking(value)
      add_content("\n#{value} G")
      end

  #{:name=>:set_line_width, :args=>[0.72]}
  def set_line_width(value)
      add_content("\n#{value} w")
      end

  #{:name=>:set_miter_limit, :args=>[10.0]}
  def set_miter_limit(value)
      add_content("\n#{value} M")
      end

  #{:name=>:set_line_join_style, :args=>[0.0]}
  def set_line_join_style(value)
      thevalue = value.to_i
      add_content("\n#{thevalue} j")
      end

  #{:name=>:set_line_cap_style, :args=>[1.0]}
  def set_line_cap_style(value)
      thevalue = value.to_i
      add_content("\n#{thevalue} J")
      end

  #{:name=>:set_line_dash, :args=>[[], 0.0]}
  def set_line_dash(values,width)
      hash_values = values.inspect
      thecontent = "\n#{hash_values} #{width} d"
      add_content(thecontent)
      end

  #{:name=>:close_subpath, :args=>[]}
  def close_subpath()
      add_content("\n h")
      end        
    
  #{:name=>:stroke_path, :args=>[]}
  def stroke_path()
      add_content("\n S")
      end

end
